/**
 * API 配置中心
 * 统一控制 Mock API 和真实 API 的切换
 * 重构版本：简化结构，提供最优解决方案
 */

/**
 * API 模式配置
 */
export const API_MODE = {
  // Mock 开关：强制关闭，使用真实API
  USE_MOCK: false,
  
  // 请求日志：开发环境下启用
  LOG_REQUESTS: import.meta.env.MODE === 'development',
  
  // Mock API 延迟配置
  MOCK_DELAY: {
    DEFAULT: 800,     // 默认延迟
    FAST: 300,        // 快速响应
    SLOW: 1500,       // 慢速响应
    LOGIN: 1200,      // 登录延迟
  },
  
  // 环境信息
  ENVIRONMENT: {
    MODE: import.meta.env.MODE,
    IS_DEV: import.meta.env.DEV,
    IS_PROD: import.meta.env.PROD,
    USE_MOCK_OVERRIDE: import.meta.env.VITE_USE_MOCK,
  },
} as const;

/**
 * API 服务选择器
 * 根据配置选择使用 Mock 或真实 API
 */
export const createApiSelector = <T extends Record<string, any>>(
  mockApi: T,
  realApi: T,
  forceMock?: boolean
): T => {
  const useMock = forceMock !== undefined ? forceMock : API_MODE.USE_MOCK;
  
  return useMock ? mockApi : realApi;
};

/**
 * Mock 延迟工具
 */
export const mockDelay = (ms?: number): Promise<void> => {
  const delay = ms || API_MODE.MOCK_DELAY.DEFAULT;
  return new Promise(resolve => setTimeout(resolve, delay));
};

/**
 * API 工具集合 - 重构简化版
 */
export const apiModeUtils = {
  /**
   * 基础工具方法
   */
  isUsingMock: (): boolean => API_MODE.USE_MOCK,
  isDevelopment: (): boolean => API_MODE.ENVIRONMENT.IS_DEV,
  isProduction: (): boolean => API_MODE.ENVIRONMENT.IS_PROD,
  
  /**
   * 获取当前 API 模式信息
   */
  getApiModeInfo: () => ({
    useMock: API_MODE.USE_MOCK,
    logRequests: API_MODE.LOG_REQUESTS,
    environment: API_MODE.ENVIRONMENT,
    mockDelay: API_MODE.MOCK_DELAY,
  }),



  /**
   * 智能API包装器 - 推荐使用 ⭐️
   * 自动记录日志并执行请求，避免重复传入URL
   * @param apiName API服务名称
   * @param apiClient API客户端实例
   * @param isMock 是否为Mock API
   */
  createApi: <TApiClient = any>(apiName: string, apiClient: TApiClient, isMock: boolean = false) => {
    const logRequest = (method: string, httpMethod: string, url: string) => {
      // 日志记录已移除
    };

    return {
      /**
       * GET请求 - 自动记录日志
       */
      get<T = any>(method: string, url: string, config?: any): Promise<T> {
        return (apiClient as any).get(url, config);
      },

      /**
       * POST请求 - 自动记录日志
       */
      post<T = any>(method: string, url: string, data?: any, config?: any): Promise<T> {
        return (apiClient as any).post(url, data, config);
      },

      /**
       * PUT请求 - 自动记录日志
       */
      put<T = any>(method: string, url: string, data?: any, config?: any): Promise<T> {
        return (apiClient as any).put(url, data, config);
      },

      /**
       * DELETE请求 - 自动记录日志
       */
      delete<T = any>(method: string, url: string, config?: any): Promise<T> {
        return (apiClient as any).delete(url, config);
      },

      /**
       * PATCH请求 - 自动记录日志
       */
      patch<T = any>(method: string, url: string, data?: any, config?: any): Promise<T> {
        return (apiClient as any).patch(url, data, config);
      }
    };
  },

  /**
   * 简化版日志记录（向后兼容）
   */
  log: (apiName: string, method: string, isMock: boolean = false) => {
    // 日志记录已移除
  },
};

// 类型导出
export type ApiModeUtils = typeof apiModeUtils;

